import { container, singleton } from "tsyringe";
import { RobotAppPage } from "../../page";
import { Task } from "../../../autox/task";
import { Thread } from "threads";
import { Context } from "../../context";
import { $ } from "../../../utils";

@singleton()
export class PhotoDetailActivity extends RobotAppPage {
    title: string = `关注直播/视频`
    private isRuning: boolean = false;
    private task: Task;
    private ctx: Context;
    constructor() {
        super()
        this.ctx = container.resolve(Context)
        this.task = container.resolve(Task)
    }
    dispose(): void {
        this.out()
    }
    is(packageName: string): boolean {
        return `com.smile.gifmaker` === packageName || `com.kuaishou.nebula` === packageName;
    }
    accept(activityName: string): boolean {
        return `com.yxcorp.gifshow.detail.PhotoDetailActivity` === activityName;
    }
    private thread: Thread;
    private hasRed: boolean = false;
    in(): boolean {
        if (this.isRuning) return;
        this.isRuning = true;
        this.thread = this.task.start(() => {
            while (this.isRuning) {
                this.count += 1;
                log(this.count)
                this.tryGetLiveNameText()
                this.ctx.updateUi({ tip: `第${this.count + 1}次检查房间是否变化` })
                sleep(1000 * 3)
                if (this.count % 10 === 0) {
                    this.count = 1;
                    $.swipeUp()
                }
            }
        })
        log(`PhotoDetailActivity In`)
        return true;
    }
    count: number = 0;
    prevLiveNameText: string;
    status: string = ``;
    private reset() {
        this.status = ``
        this.prevLiveNameText = ``
        this.count = 0;
    }
    private tryGetLiveNameText() {
        const liveNameText = id('live_name_text').visibleToUser(true).findOnce()
        if (liveNameText) {
            const text = liveNameText.text()
            if (this.prevLiveNameText !== text) {
                this.ctx.updateUi({ currentRoom: text })
                this.prevLiveNameText = text;
                this.count = 1;
                // 房间变化了 执行任务
                this.ctx.updateUi({
                    toast: `房间变更，正在检查有无红包`,
                    currentStatus: '正在检查有无红包'
                })
                this.tryClickLiveRedPacketStatusText()
            }
        }
    }
    private tryClickLiveRedPacketStatusText() {
        /**
         * 尝试点击红包按钮
         * id("live_red_packet_status_text")
         */
        const liveRedPacketStatusText = id("live_red_packet_status_text").visibleToUser(true).findOnce()
        if (liveRedPacketStatusText) {
            $.clickButton(liveRedPacketStatusText)
            this.ctx.updateUi({
                toast: `此房间有红包`,
                currentResult: `有红包，${liveRedPacketStatusText.text()}`,
                currentStatus: `检测完毕`
            })
        } else {
            this.ctx.updateUi({
                currentStatus: `检测完毕`,
                currentResult: `此房间，暂无红包`,
                toast: `暂无发现红包`
            })
        }
    }
    run() {
        while (this.isRuning) {
            sleep(1000)
        }
    }
    out(): boolean {
        if (!this.isRuning) return false;
        this.isRuning = false;
        this.reset();
        if (this.thread && this.thread.isAlive()) {
            this.thread.interrupt();
        }
        log(`PhotoDetailActivity out`)
        return true;
    }
}